package com.wondersgroup.wuhan_demo.ui.fk;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wondersgroup.wuhan_demo.R;
import com.wondersgroup.wuhan_demo.util.IdCardUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.mrobot.pkgcore.cookyplus.idcard.MIdCardReader;
import cn.mrobot.pkgcore.cookyplus.idcard.bean.Birthday;
import cn.mrobot.pkgcore.cookyplus.idcard.bean.IdCardInfo;
import cn.mrobot.pkgcore.cookyplus.idcard.cooky.CookyIdCardReader;
import cn.mrobot.pkgcore.cookyplus.idcard.listener.IdCardReadListener;

import static com.wondersgroup.wuhan_demo.ui.fk.ScanIDCardActivity.idCard;

public class ScanIDCardFragment extends Fragment {


    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.tv_sfzhm)
    TextView tvSfzhm;
    @BindView(R.id.tv_xm)
    TextView tvXm;
    @BindView(R.id.tv_xb)
    TextView tvXb;
    @BindView(R.id.tv_mz)
    TextView tvMz;
    @BindView(R.id.tv_csrq)
    TextView tvCsrq;
    @BindView(R.id.btn_sure_fk)
    Button btnSureFk;
    @BindView(R.id.ll_user_info)
    ConstraintLayout llUserInfo;
    Unbinder unbinder;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    private String TAG = this.getClass().getSimpleName();
    IdCardInfo mIdCardInfo;
    private IdCardReadListener mIdCardReadListener = new IdCardReadListener() {
        @Override
        public void onReceiveIdCardInfo(IdCardInfo idCardInfo) {
            Toast.makeText(getContext(), "已经读取到身份证数据", Toast.LENGTH_SHORT).show();
            mIdCardInfo = idCardInfo;
            setUserInfoVisible();
            showCardInfo(idCardInfo);
        }
    };
    private MIdCardReader mIdCardReader;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_scan_idcard, container, false);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mIdCardReader = CookyIdCardReader.get();
        CookyIdCardReader.get().init(getContext());
        setOpen();
        readCard();
//        setUserInfoVisible();
        setTipVisible();
    }

    private void setTipVisible() {
        tvTip.setVisibility(View.VISIBLE);
        llUserInfo.setVisibility(View.GONE);
    }

    private void setUserInfoVisible() {
        tvTip.setVisibility(View.GONE);
        llUserInfo.setVisibility(View.VISIBLE);
    }

    private void showCardInfo(IdCardInfo idCardInfo) {
        tvSfzhm.setText(IdCardUtil.tmIdCard(idCardInfo.getCardId()));
        tvXm.setText(idCardInfo.getName());
        tvXb.setText(idCardInfo.getSex());
        tvMz.setText(idCardInfo.getNation());
        tvCsrq.setText(idCardInfo.getBirthday().getRawStr());
    }

    private void setOpen() {
        boolean openResult = mIdCardReader.open();
        if (openResult) {
            Log.i(TAG, "setOpen: 打开成功");
        } else {
            Log.i(TAG, "setOpen: 打开失败");
        }
    }

    private void readCard() {
        boolean readResult = mIdCardReader.read(mIdCardReadListener);
        if (readResult) {
            Log.i(TAG, "readCard: 读命令发送成功");
        } else {
            Log.i(TAG, "readCard: ;读命令发送失败");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        CookyIdCardReader.get().close();
        unbinder.unbind();
    }

    private void setClose() {
        mIdCardReader.close();
        Log.i(TAG, "setClose: 已关闭");
    }

    @OnClick(R.id.btn_sure_fk)
    public void onViewClicked() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), OfferCardActivity.class);
        if (mIdCardInfo != null) {
            IdCardInfo idCardinfo=new IdCardInfo();
            idCardinfo.setCardId(mIdCardInfo.getCardId());
            idCardinfo.setBirthday(mIdCardInfo.getBirthday());
            idCardinfo.setName(mIdCardInfo.getName());
            idCardinfo.setNation(mIdCardInfo.getNation());
            idCardinfo.setSex(mIdCardInfo.getSex());
            intent.putExtra(idCard, idCardinfo);
            startActivity(intent);
            getActivity().finish();
        } else {
//            mIdCardInfo=new IdCardInfo();
//            mIdCardInfo.setCardId("411424199208104222");
//            mIdCardInfo.setBirthday(new Birthday("19920810"));
//            mIdCardInfo.setName("李亚清");
//            mIdCardInfo.setNation("汉族");
//            mIdCardInfo.setSex("女");
//            intent.putExtra(idCard, mIdCardInfo);
//            startActivity(intent);
            Toast.makeText(getContext(), "未扫描出身份信息", Toast.LENGTH_SHORT).show();
        }
    }
}
